/**
 * Created by hongye.liu on 14-10-29.
 * 改变线路
 */
define(['jquery','underscore','backbone','global','./busSearchResult','text!/android_asset/busAppWeb/module/busApp/busChange/busChange.html'],
    function($,_,Backbone, global,busSearchResult ,html){
        var busChangeView = Backbone.View.extend({
            template: _.template(html),
            siteStart : null,
            siteEnd : null,
            initialize:function(data){
                this.render();
                this.addEvent();
                this.addAutoCompleteCommand();
            },
            render:function(){
                $('div#contentID').html(this.template);
                $('body').trigger("create");
            },

            addEvent:function(){
                var _this = this;
                $('#queryBtn').click(function(){
                    if($('#startSite')[0].value.replace(/\s/gi,'') == '' || $('#endSite')[0].value.replace(/\s/gi,'') == '')
                    {
                        alert("查询条件不完整");
                        return;
                    }else{
                        var busSearchResultView = new busSearchResult();
                        Backbone.history.navigate('#searchResultPage',true);
                        busSearchResultView.search(_this.siteStart, _this.siteEnd);
                    }
                })
            },

            addAutoCompleteCommand:function(){
                    var acStart = new BMap.Autocomplete(    //建立一个自动完成的对象
                        {
                            "input" : "startSite"
                            ,"location" : global.map
                        });
                    var acEnd = new BMap.Autocomplete(    //建立一个自动完成的对象
                        {
                            "input" :"endSite"
                            ,"location" : global.map
                        });
                    this.setAutoComplete(acStart, "start");
                    this.setAutoComplete(acEnd, "end");
            },

            setAutoComplete:function(ac, site){
                ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
                    var str = "";
                    var _value = e.fromitem.value;
                    var value = "";
                    if (e.fromitem.index > -1) {
                        value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    }
                    str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

                    value = "";
                    if (e.toitem.index > -1) {
                        _value = e.toitem.value;
                        value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    }
                    str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
                    $('#searchResultPanel').innerHTML = str;
                });
                var myValue;
                var _this = this
                ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
                    var _value = e.item.value;
                    myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    $("#searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
                    _this.getPoint(site,myValue);
                });
            },

            getPoint:function(site, siteDetail) {
                global.map.clearOverlays();    //清除地图上所有覆盖物
                var _this = this;
                function pointCommand() {
                    var point = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                    if(site == "start"){
                        _this.siteStart = point;
                    }
                    if(site == "end"){
                        _this.siteEnd = point;
                    }
                    if(site == "site"){
                        global.remindSiteList[siteDetail] = point;
                    }
                }
                var local = new BMap.LocalSearch(global.map, { //智能搜索
                    onSearchComplete: pointCommand
                });
                local.search(siteDetail);
            }

        });
        return busChangeView;

    }
)